<?php
namespace App\Models\PartnerModels;
use App\Models\ParentModel;
use DB;
use App\Components\Tools;
/**
 * 合伙人model
 * @author puppet
 */
class Keyword extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_keyword';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_keyword';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;

    /**
     * 翻页展示所有信息
     */
    public function getPageData($params)
    {
        //   echo '<pre/>'; print_r($params);exit();
        $res = false;
        $limit_num = (isset($params['limit']) && intval($params['limit']) > 0 && intval($params['limit']) <= 100) ? $params['limit'] : 20;
        $limit_sta = 0;
        $page = 1;
        if (isset($params['page']) && intval($params['page']) > 0) {
            $limit_sta = intval($limit_num * intval($params['page'] - 1));
            $page = intval($params['page']);
        }
        $cou_sql = "SELECT count(*) as count FROM {$this->db_table} WHERE status >= ? and user_id=?";
        $sql = "SELECT id,name as search_name,user_id,update_time,input_time,status FROM {$this->db_table} WHERE status >= ? and user_id=?";
        $user_id = isset(session('vip_user')[0]['admin_id']) ? session('vip_user')[0]['admin_id'] : 0;
        $values = [0, $user_id];

        $count = DB::select($cou_sql, $values);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;

        //  print_r($count);exit();
        if (isset($params['order']) && !empty($params['order'])) {
            $order_db = ['id'];
            $sql .= " ORDER BY ";
            foreach ($params['order'] as $order) {
                $result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
                !empty($result) && $sql .= $result . ' ' . $order['dir'] . ',';
            }
            $sql = substr($sql, 0, -1);
        }
        $sql .= " LIMIT $limit_sta,$limit_num";
        $data_list = DB::select($sql, $values);
        // echo '<pre/>';print_r($data_list);

        $res['data'] = $data_list;
        $res['total'] = $count;
        //总条数记录
        return $res;
    }

    //修改状态
    function updateTal($kid, $data)
    {
        $sql = "update $this->table set ";
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $sql .= "`{$k}`='{$v}',";
            }
            $sql = substr($sql, 0, -1) . " where id={$kid}";
        }
        // echo $sql;exit;
        return DB::update($sql);
    }

    //关键词计数
    function selectCount($user_id)
    {
        $sql = "select count(id) as count from pt_keyword where user_id=? and status!=-1";
        $re = DB::select($sql, [$user_id]);
        return (isset($re[0]['count']) ? $re[0]['count'] : false);
    }

    //判断该关键词是否存在
    function findKeyword($user_id, $name)
    {
        $sql = "select id from pt_keyword where user_id=? and name='{$name}' and status!=-1";
        //    print_r($sql);exit();
        $re = DB::select($sql, [$user_id]);
        return isset($re[0]['id']) ? $re[0]['id'] : false;
    }



    //拉取关键词   type 0内容关键词   否则 标题关键词
    function getKeyData($type,$userid){
        $html='<tr><td colspan="10" style="padding:10px;">无数据</td><tr>';
        $user_id=isset($userid) ? $userid : 0;
        if($type){
            $sql="select * from pt_keyword where user_id = ? and status!=-1 order by status desc,update_time desc";
            $re=DB::select($sql,[$user_id]);
            $re=isset($re[0]['id']) ? $re : false;
        }else{
            $sql="select * from pt_keyword_conent where user_id = ? and status!=-1 order by status desc,update_time desc";
            $re=DB::select($sql,[$user_id]);
            $re=isset($re[0]['id']) ? $re : false;
        }
        if($re){
            $html='';
            foreach($re as $k=>$v){
                $html.="<tr>";
                $html.="<td>{$v['name']}</td>";
                $html.="<td>{$v['update_time']}</td>";
                $statusType=$v['status']==1 ? 'checkbox' : 'default';
                $status=$v['status']==1 ? 0 : 1;
                $html.="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"{$statusType}\" checked=\"\" onclick=\"upStatus({$v['id']},{$status},{$type},this)\" ><span></span></label></div></td>";
                $html.="<td>";
                $conText="是否删除：标题关键词【{$v['name']}】";
                if(!$type){
                    $html.="<a href=\"javascript:;\" title=\"修改\" aria-label=\"Update\" data-pjax=\"0\" onclick=\"addKey({$type},{$v['id']},'{$v['name']}',)\" ><span class=\"glyphicon glyphicon-pencil\"></span></a>";
                    $html.="&nbsp;&nbsp;";
                    $conText="是否删除：内容关键词【{$v['name']}】";
                }
                $html.="<a href=\"javascript:;\" title=\"删除\" aria-label=\"Delete\" data-pjax=\"0\" onclick=\"del({$v['id']},-1,{$type},'{$conText}')\" ><span class=\"glyphicon glyphicon-trash\"></span></a>";
                $html.="</td>";
                $html.="</tr>";
            }
        }
        return ['code'=>0,'html'=>$html];
    }

    //修改关键词状态	type 0内容关键词   否则 标题关键词
    function upStatusKey($userid,$kid,$status,$type){
        $user_id=isset($userid) ? $userid : 0;
        $update_time=date('Y-m-d H:i:s');
        if($type){
            $open=$this->getKeyDataUp();
            if($status==1 && $open>=15){
                return 99;
            }
            $re=DB::table('pt_keyword')->where('id',$kid)->where('user_id',$user_id)->update(['status'=>$status,'update_time'=>$update_time]);
        }else{
            $re=DB::table('pt_keyword_conent')->where('id',$kid)->where('user_id',$user_id)->update(['status'=>$status,'update_time'=>$update_time]);
        }
        return $re;
    }

    //查询标题关键词是否开启上限
    function getKeyDataUp(){
        $user_id=isset(session('vip_user')[0]['admin_id']) ? session('vip_user')[0]['admin_id'] : 0;
        $sql="select count(id) as count from pt_keyword where status=1 and user_id = ?";
        $re=DB::select($sql,[$user_id]);
        return isset($re[0]['count']) ? $re[0]['count'] : 0;
    }

    //查询关键词是否存在	type 0内容关键词   否则 标题关键词
    function checkKey($type,$name,$kid){
        $user_id=isset(session('vip_user')[0]['admin_id']) ? session('vip_user')[0]['admin_id'] : 0;
        if($type){
            $sql="select id from pt_keyword where status!=-1 and user_id = ? and name='{$name}'";
            if($kid){
                $sql.=" and id!={$kid}";
            }
            $re=DB::select($sql,[$user_id]);
            $re=isset($re[0]['id']) ? $re[0]['id'] : false;
        }else{
            $sql="select id from pt_keyword_conent where status!=-1 and user_id = ? and name='{$name}'";
            if($kid){
                $sql.=" and id!={$kid}";
            }
            $re=DB::select($sql,[$user_id]);
            $re=isset($re[0]['id']) ? $re[0]['id'] : false;
        }
        return $re;
    }

    //保存关键词	type 0内容关键词   否则 标题关键词
    function saveKey($kid,$data,$type){
        $re=false;
        if($type){
            if($kid){
                $re=DB::table('pt_keyword')->where('id',$kid)->update($data);
            }else{
                $data['create_time']=date('Y-m-d H:i:s');
                $re=DB::table('pt_keyword')->insertGetId($data);
            }
        }else{
            if($kid){
                $re=DB::table('pt_keyword_conent')->where('id',$kid)->update($data);
            }else{
                $data['create_time']=date('Y-m-d H:i:s');
                $re=DB::table('pt_keyword_conent')->insertGetId($data);
            }
        }
        return $re;
    }
    //保存关键词	type 0内容关键词   否则 标题关键词
    function saveApiKey($data,$type){
        $re=false;
        if($type){
            $re=DB::table('pt_keyword')->insertGetId($data);
        }else{
            $re=DB::table('pt_keyword_conent')->insertGetId($data);
        }
        return $re;
    }
    //修改询盘评论 处理状态
    function editStatus($kid,$status){
        return DB::table('pt_partner_comment')->where('id',$kid)->update(['status'=>$status]);
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}